﻿using G.APITest.Config;
using System;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace G.APITest.Part
{
    [Export(typeof(G.APITest.Part.IAPIRequestPart))]
    public class NormalRequest : G.APITest.Part.IAPIRequestPart
    {
        private KVPair[] extensionProjectProperties;
        public KVPair[] ExtensionProjectProperties
        {
            get
            {
                return extensionProjectProperties;
            }
            protected set
            {
                extensionProjectProperties = value;
            }
        }

        private void RaiseStatus(string str)
        {
            if (RaiseStatusCallBack != null)
                RaiseStatusCallBack(str);
        }
        public Action<string> RaiseStatusCallBack
        {
            get;
            set;
        }

        protected virtual void DoSign(APIProjectConfig config, APIConfig api, System.Collections.Generic.Dictionary<string, string> paramDic)
        {

        }
        public void CreateRequestAsync(APIProjectConfig config, APIConfig api, Action<G.SL.Net.HttpRequest> callback)
        {

            string uri = config.APIBaseUri + "/" + api.Path;
            var paramDic = api.GetAllParamters(config);
            DoSign(config, api, paramDic);
            RaiseStatus("API请求地址：" + uri);
            if (!api.IsPost)
            {
                var get = new G.SL.Net.HttpGet(uri);
                foreach (var p in paramDic)
                {
                    get.Params.Add(p.Key, p.Value);
                }
                RaiseStatus("API请求参数：" + get.GetRequestParam());

                AppendHeader(config, api, get);
                callback(get);
            }
            else
            {
                var filePostParam = api.Paramters.FirstOrDefault(c => c.ParamterType == ParamterType.ByteArray);
                if (filePostParam != null)
                {
                    G.SL.Net.HttpFileUpload post = new G.SL.Net.HttpFileUpload(uri);
                    foreach (var p in paramDic)
                    {
                        post.Params.Add(p.Key, p.Value);
                    }
                    AddFileChildWindow w = new AddFileChildWindow();
                    w.Closed += (s, e) =>
                    {
                        if (w.DialogResult == true)
                        {
                            w.FileInfo.Name = filePostParam.Name;
                            post.Files = new G.SL.Net.FileInfo[] { w.FileInfo };
                            AppendHeader(config, api, post);
                            callback(post);
                        }
                    };
                    w.Show();
                }
                else
                {
                    G.SL.Net.HttpPost post = new SL.Net.HttpPost(uri);
                    post.ContentType = "application/x-www-form-urlencoded";
                    string postString = string.Join("&", paramDic.Select(c => string.Format("{0}={1}", c.Key, Uri.EscapeDataString(c.Value))));
                    post.PostData = postString.GetBytes();
                    RaiseStatus("API Post字符串：" + postString);
                    AppendHeader(config, api, post);
                    callback(post);
                }
            }
            //else
            //{
            //    bool isFile=api.Paramters.
            //}

        }

        private static void AppendHeader(APIProjectConfig config, APIConfig api, SL.Net.HttpRequest get)
        {
            foreach (var item in config.GlobalHeaders)
            {
                get.Headers[item.Key] = item.Value;
            }
            foreach (var item in api.Headers)
            {
                get.Headers[item.Key] = item.Value;
            }
        }
    }
}
